//
//  AccountViewController.m
//  iMoney
//
//  Created by Sean McGrail on 1/29/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "QuickViewController.h"
#import "DatePickerController.h"
#import "Account.h"
#import "iMoneyAppDelegate.h"
#import "Expense.h"
#import "OverlayView.h"

@implementation QuickViewController
@synthesize name, type,  amount, date, note;
@synthesize selectedIndex;
@synthesize table;

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
	id object = [super initWithNibName:nibName bundle:nibBundle];
	
	self.tabBarItem.image = [UIImage imageNamed:@"wallet.png"];
	self.title = @"Quick Add";
	
	return object;
}

- (void)viewDidLoad {
	
	self.view.backgroundColor =
	[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"gradientBackground.png"]];
	
	table.backgroundColor = 
	[UIColor clearColor];
	
	
	//disable input from date field
	date.enabled = NO;
	
	
	//fail fast
	selectedIndex=-1;
	
	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]
									   init] autorelease];
	
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	
	date.text = [dateFormatter stringFromDate: [NSDate date]];
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[table reloadData];
}


- (IBAction)typePressed
{
	//type list
}

- (IBAction)expensePressed
{
	//expense list
	
}


-(IBAction) addPressed
{
	
	if([name.text length] > 0 && [type.text length] > 0 && selectedIndex>=0)
	{
	
		Expense* e = [[Expense alloc] initAll:[NSDate date] :name.text :type.text :[amount.text doubleValue]];
		Account* a = [[iMoneyAppDelegate getMoneyState] getAccount:selectedIndex];
		[a addExpense:e:NO];
		[e release];
		
		NSString* message = [[NSString alloc] initWithFormat:@"%@$ Added to %@",amount.text,  a.accountName];
		
		UIAlertView *myAlertView = [[UIAlertView alloc] 
									initWithTitle:@"Added" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];	
		
		[myAlertView show];
		[myAlertView release];
	}
	else
	{
		OverlayView *loadingView =
		[OverlayView loadingViewInView:self.view :@"Empty Values"];
		
		
		[loadingView
		 performSelector:@selector(removeView)
		 withObject:nil
		 afterDelay:1];
		
	}
	
	
	
	
}



//------------------------


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	return YES;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
	return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
	
	
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
	if([text compare:@"\n"] == 0) //check for return entered
	{
		[textView resignFirstResponder]; //close keyboard on screen
	}
	
	return YES;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{	
	if([string compare:@"\n"] == 0) //check for return entered
	{
		[textField resignFirstResponder]; //close keyboard on screen
	}
	
	return YES;
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


//----------------- table actions

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[iMoneyAppDelegate getMoneyState] numAccounts];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"QuickCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	//diable highlighting
	cell.selectionStyle = UITableViewCellSelectionStyleNone;

	//draw the checkmark if selected
	if(selectedIndex == indexPath.row)
	{
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	}
	else
		cell.accessoryType = UITableViewCellAccessoryNone;
    
	[cell setBackgroundColor:[UIColor lightGrayColor]];
	
	cell.textLabel.textColor = [UIColor blackColor];
	cell.textLabel.text = [[[iMoneyAppDelegate getMoneyState] getAccount:indexPath.row] accountName];
	
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	selectedIndex = indexPath.row;
	[self.table reloadData];

}




- (void)dealloc {
	
	[table release];
	[amount release];
	[name release];
	[type release];
	[note release];
    [super dealloc];
}


@end

